package me.numark.fragments;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import me.numark.R;
import me.numark.activities.LogInActivity;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.Settings;
import me.numark.view.CircleImageView;

/**
 * Created by ytian on 15/8/8.
 */
public class InputDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";

    public static final String ARG_TEXT = "text";

    private static final String ARG_HINT = "hint";

    private static final String ARG_MSG = "msg";

    private static final String ARG_TYPE = "type";

    public static final int TYPE_EDIT_LIGHT = 0;

    public static final int TYPE_RESET_PASSWORD = 1;

    public static final int TYPE_REMOTE_PWD = 2;

    private String title, text, hint, msg;

    private int type;

    InputDialogInterface mListener;

    public static InputDialogFragment newInstance(String title, String text, String hint,
                                                  String msg, int type) {
        InputDialogFragment fragment = new InputDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_TEXT, text);
        args.putString(ARG_HINT, hint);
        args.putString(ARG_MSG, msg);
        args.putInt(ARG_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    public InputDialogFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE);
            text = getArguments().getString(ARG_TEXT);
            hint = getArguments().getString(ARG_HINT);
            msg = getArguments().getString(ARG_MSG);
            type = getArguments().getInt(ARG_TYPE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof InputDialogInterface) {
            mListener = (InputDialogInterface) activity;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Dialog lDialog = new Dialog(getActivity(),R.style.dialog_style);
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(R.layout.r_okcanceldialogview);
        final EditText et = (EditText) lDialog.findViewById(R.id.dialog_edit);
        et.setVisibility(View.VISIBLE);
        et.setText(text);
        et.setHint(hint);
        if (!TextUtils.isEmpty(text)) {
            et.setSelection(text.length());
        }
        TextView txtMsg = (TextView) lDialog.findViewById(R.id.dialog_message);
        txtMsg.setVisibility(View.GONE);
        TextView txtTitle = (TextView)lDialog.findViewById(R.id.dialog_title);
        txtTitle.setVisibility(View.VISIBLE);
        txtTitle.setText(title);
        ((Button) lDialog.findViewById(R.id.cancel))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        lDialog.dismiss();
                        if (getTargetFragment() != null) {
                            getTargetFragment().onActivityResult(getTargetRequestCode(),
                                    Activity.RESULT_CANCELED,
                                    getActivity().getIntent());
                        }
                    }
                });
        ((Button) lDialog.findViewById(R.id.ok))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        lDialog.dismiss();
                        String data = et.getText().toString().trim();
                        if (TextUtils.isEmpty(data)) {
                            Toast.makeText(getActivity(),
                                    getActivity().getString(R.string.input_dialog_empty),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Intent intent = new Intent();
                        intent.putExtra(ARG_TEXT, data);
                        if (getTargetFragment() != null) {
                            getTargetFragment().onActivityResult(getTargetRequestCode(),
                                    Activity.RESULT_OK,
                                    intent);
                        }
                        if (mListener != null) {
                            mListener.onInputDialogTextDone(data, type);
                        }
                    }
                });
        lDialog.setCancelable(true);
        if (!TextUtils.isEmpty(msg)) {
            txtMsg.setVisibility(View.VISIBLE);
            txtMsg.setText(msg);
        }
        return lDialog;
    }

    public static interface InputDialogInterface {

        public void onInputDialogTextDone(String text, int type);
    }

}
